#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include <errno.h>
#include <sys/mman.h>
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <jpeglib.h>

typedef struct {
    int width;
    int height;
    int depth;
}Image_format;
// 
class Image{
public:
    Image(int width, int height,int depth);
    Image(char* filename);

    bool Init(uint8_t* head);
    uint8_t at(int w, int h, int c);
    void setAt(int w, int h, int c, int value);
    bool saveMe(const char* filename, int quality) const;
    std::unique_ptr<uint8_t[]> Myhead;
    unsigned long long int MybyteUsed;
    Image_format Myformat;
    void drawRec(int x1, int y1, int x2, int y2);

private:
    bool Myimread(char *filename);
    
    bool Inited = false;
};

class Vcam{
public:
    Vcam(char* device_name, int width, int height, int fps);
    ~Vcam();
    bool Init();
    void printCamInfo();
    Image* getNextFrame();

private:
    int Myfd, Mydevice;
    int Mywidth, Myheight, Myfps;
    bool Inited = false;

    void* Myfrm_base[3];  //映射后的用户空间的首地址
    unsigned int Myfrm_size[3];
    struct v4l2_buffer Myreadbuffer;

    Image* decodeMjpeg(uint8_t *mjpeg_data, unsigned long mjpeg_size);
};


